package com.cwd.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.cwd.dao.CheckGroupDao;
import com.cwd.entity.PageResult;
import com.cwd.entity.QueryPageBean;
import com.cwd.pojo.CheckGroup;
import com.cwd.pojo.CheckItem;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.cwd.service
 * @ClassName: CheckGroupServiceImpl
 * @Author: CWD
 * @Date: 2020/3/1 22:22
 * @Description:
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    //新增检查组
    @Override
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
        checkGroupDao.add(checkGroup);
        Integer groupId = checkGroup.getId();
        this.setCheckgroupAndCheckitem(groupId,checkitemIds);
    }
    //分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupDao.pageQuery(queryString);
        long total = page.getTotal();
        List<CheckGroup> groupList = page.getResult();
        return new PageResult(total,groupList);
    }
    //编辑页面检查组的信息回显
    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return checkGroup;
    }
    //编辑页面查询当前检查组对应的检查项信息
    @Override
    public List<Integer> findItemsByGroupId(Integer id) {
        List<Integer> checkItemList = checkGroupDao.findItemsByGroupId(id);
        return checkItemList;
    }
    //编辑确认按钮
    @Override
    public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        //先修改检查组的信息
        checkGroupDao.updateCheckGroup(checkGroup);
        //再删除中间表的关系
        checkGroupDao.deleteReferGroupToItem(checkGroup.getId());
        //最后重新设置中间表关系
        setCheckgroupAndCheckitem(checkGroup.getId(),checkitemIds);
    }
    //删除检查组
    @Override
    public void deleteCheckgroup(Integer id) {
        int i = id;
        checkGroupDao.deleteCheckgroup(id);
    }
    //查询所有检查组信息
    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroupList = checkGroupDao.findAll();
        return checkGroupList;
    }

    //设置检查组和检查项的对应关系
    public void setCheckgroupAndCheckitem(Integer groupId,Integer[] checkitemIds){
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroupId",groupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckgroupAndCheckitem(map);
            }
        }
    }
}
